// 通过vue-router插件实现模板路由配置
import { createRouter,createWebHashHistory } from "vue-router";
import { constantRoute } from "./routes"
import { usePermissStore } from '@/store/permiss'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
// 创建路由
let router = createRouter({
    // 路由模式 hash
    history: createWebHashHistory(),
    routes: constantRoute,
    // 滚动行为
    scrollBehavior() {
        return {
            left: 0,
            top: 0
        }
    }
});
router.beforeEach((to, from, next) => {
    NProgress.start();
    const token = JSON.parse(localStorage.getItem('TOKEN'))
    const role = token ?.role
    const permiss = usePermissStore();
    if (!token && to.path != '/login') {
        next('/login');
    } else if (!role && to.meta.noAuth !== true && to.path != '/login') {
        next('/login');
    } else if (typeof to.meta.permiss == 'string' && !permiss.key.includes(to.meta.permiss)) {
        // 如果没有权限，则进入403
        next('/403');
    } else {
        next();
    }
});

router.afterEach(() => {
    NProgress.done();
});
export default router;